using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PocketLibrary.Management;

namespace PocketApps
{
    public partial class voo2doForm : Form
    {
        PocketLibrary.Management.voo2do myvoo2do = new PocketLibrary.Management.voo2do();
        List<voo2doView> views = new List<voo2doView>();
        public voo2doForm()
        {
            InitializeComponent();
        }
        #region voo2do methods

        private void tvViews_AfterSelect(object sender, TreeViewEventArgs e)
        {

            if (e.Node.GetType() == typeof(v2doViewNode))
            {
                v2doViewNode v2view = (v2doViewNode)e.Node;
                tvProjects.Nodes.Clear();
                cbV2DProjects.Items.Clear();
                foreach (voo2doProject proj in v2view.v2doView.ProjectsInView)
                {
                    v2doProjectNode v2projNode = new v2doProjectNode();
                    v2projNode.v2doProject = proj;
                    v2projNode.Text = proj.ProjectName;
                    tvProjects.Nodes.Add(v2projNode);
                    cbV2DProjects.Items.Add(proj.ProjectName);

                }
            }
            else if (e.Node.GetType() == typeof(v2doProjectNode))
            {
                // switch to project view
                tcVoo2do.SelectedIndex = 1;
            }
        }

        private void tvProjects_AfterSelect(object sender, TreeViewEventArgs e)
        {
            v2doProjectNode v2pNode = (v2doProjectNode)e.Node;
            List<voo2doTask> tasks = myvoo2do.getAllTasksForProject(v2pNode.v2doProject.ProjectId);
            tvTasks.Nodes.Clear();
            foreach (voo2doTask task in tasks)
            {
                v2doTaskNode v2task = new v2doTaskNode();
                v2task.v2doTask = task;
                v2task.Text = task.TaskDescription;
                // check task if already completed


                TimeSpan compare = new TimeSpan(task.deadline.Ticks - DateTime.Now.Ticks);
                if (compare.Days < 3)
                    v2task.BackColor = Color.Red;
                else if (compare.Days >= 3 && compare.Days < 7)
                    v2task.BackColor = Color.Yellow;
                else
                    v2task.BackColor = SystemColors.Window; //Color.LightGreen;

                if (task.completed < DateTime.Now)
                {
                    v2task.BackColor = Color.White;
                    v2task.ForeColor = Color.LightGray;

                    v2task.Checked = true;
                }

                tvTasks.Nodes.Add(v2task);
            }

            // switch to task view
            tcVoo2do.SelectedIndex = 2;
        }

        private void bnVoo2DoClose_Click(object sender, EventArgs e)
        {
            this.SuspendLayout();

            this.ResumeLayout();
        }

        private void tbTaskDesc_TextChanged(object sender, EventArgs e)
        {

        }

        private void tbTaskDesc_LostFocus(object sender, EventArgs e)
        {
            if (tbTaskDesc.Text == "" || tbTaskDesc.Text == "task description")
            {
                tbTaskDesc.ForeColor = Color.Silver;
                tbTaskDesc.Text = "task description";
            }
            else
                tbTaskDesc.ForeColor = Color.Black;
        }

        private void tbTaskDesc_GotFocus(object sender, EventArgs e)
        {
            tbTaskDesc.ForeColor = Color.Black;
            tbTaskDesc.SelectAll();
        }

        private void tvTasks_AfterSelect(object sender, TreeViewEventArgs e)
        {
            v2doTaskNode v2task = (v2doTaskNode)e.Node;
            cbV2DProjects.Text = v2task.v2doTask.projName;
            tbTaskDesc.Text = v2task.v2doTask.TaskDescription;
            dtpVoo2do.Value = v2task.v2doTask.deadline;

            if (v2task.v2doTask.priority > nudPriority.Minimum || v2task.v2doTask.priority < nudPriority.Maximum)
                nudPriority.Value = v2task.v2doTask.priority;
            else
                nudPriority.Value = nudPriority.Minimum;

        }
        #endregion

        private void voo2doForm_Load(object sender, EventArgs e)
        {
            myvoo2do.OnLoggedIn += new EventHandler(myvoo2do_OnLoggedIn);
            // implement username and password to datafile
            myvoo2do.Username = "";
            myvoo2do.Password = "";
            this.ResumeLayout();
            if (!myvoo2do.LoggedIn)
                myvoo2do.LogIn();
        }

        void myvoo2do_OnLoggedIn(object sender, EventArgs e)
        {
            //MessageBox.Show("You have successfully logged in.", "voo2do");
            tvViews.Nodes.Clear();

            views = myvoo2do.getViews();

            foreach (voo2doView view in views)
            {
                v2doViewNode tnView = new v2doViewNode();
                tnView.Text = view.ViewName;
                tnView.v2doView = view;

                foreach (voo2doProject proj in view.ProjectsInView)
                {
                    v2doProjectNode tnProj = new v2doProjectNode();
                    tnProj.Text = proj.ProjectName;
                    tnView.Nodes.Add(tnProj);
                }

                tvViews.Nodes.Add(tnView);
            }

        }
    }
    #region voo2do
    public enum voo2doNodeType { View, Project, Task };

    public class v2doViewNode : TreeNode
    {
        public voo2doNodeType voo2doType = voo2doNodeType.View;
        public voo2doView v2doView = new voo2doView();
    }

    public class v2doProjectNode : TreeNode
    {
        public voo2doNodeType voo2doType = voo2doNodeType.Project;

        public voo2doProject v2doProject = new voo2doProject();
    }

    public class v2doTaskNode : TreeNode
    {
        public voo2doNodeType voo2doType = voo2doNodeType.Task;

        public voo2doTask v2doTask = new voo2doTask();
    }
    #endregion
}